/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AsociacionRA.java
 *
 * Created on May 3, 2010, 2:51:36 AM
 */

package UI;

import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JTable;
import robot_maker.Accesorio;
import Modelo.BaseDato;
import robot_maker.ResultSetTableModel;

/**
 *
 * @author CarlosScipione
 */
public class AsociacionRA extends javax.swing.JFrame {
    private int codRobot = 1;
    private int codAccesorio = 1;
    private int fila;

    /** Creates new form AsociacionRA */
    public AsociacionRA() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

//    /** This method is called from within the constructor to
//     * initialize the form.
//     * WARNING: Do NOT modify this code. The content of this method is
//     * always regenerated by the Form Editor.
//     */

     private void addlisteners (){

     this.acc_tabla.getSelectionModel().addListSelectionListener(
             new javax.swing.event.ListSelectionListener() {
            @Override
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
               jTable1SelectionChanged2(evt);
            }
        });
    }
     private void addlistener (){

     this.robot_table.getSelectionModel().addListSelectionListener(
             new javax.swing.event.ListSelectionListener() {
            @Override
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
               jTable1SelectionChanged1(evt);
            }
        });
    }
     private void llenar (JTable tabla, String query) {
            try{
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        AsociacionRA.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            tabla.setModel(new ResultSetTableModel(query));
            if (tabla.getRowCount() > 0) {
                tabla.setRowSelectionInterval(0, 0);
            }
        } catch (ClassNotFoundException ex) {
        Sistema.loggear(36, "llenar", "NO SE ENCUENTRA LA CLASE", 
                AsociacionRA.class,
                    "ClassNotFoundException");
        } catch (SQLException ex) {
        Sistema.loggear(35, "llenar", "SQL SYNTAXIS", AsociacionRA.class,
                    "SQLException");
        }
    }




    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        robot_table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        acc_tabla = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        robot_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        robot_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                robot_tableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(robot_table);

        jLabel1.setText("Asociacion:");

        acc_tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        acc_tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                acc_tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(acc_tabla);

        jLabel2.setText("Robots:");

        jLabel3.setText("Accesorio:");

        jButton1.setText("Asociar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 554, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 248, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(jButton1)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 264, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .add(12, 12, 12))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 391, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(28, 28, 28)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jLabel2)
                                .add(8, 8, 8))
                            .add(jLabel3)))
                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jScrollPane1, 0, 0, Short.MAX_VALUE)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void robot_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_robot_tableMouseClicked
        JTable target = (JTable)evt.getSource();
        int row = target.getSelectedRow();
        int column = target.getSelectedColumn();
        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select r.id \"Codigo\", " 
                    + "r.nombre \"Nombre\", r.descripcion \"Descripcion\" "
                    + "from ROBOT r");
        } catch (DinamicaDato ex) {
        }
        int i = 0;
        while (i <= row){
            try {
                lista.next();
                i++;
            } catch (SQLException ex) {
        Sistema.loggear(35, "mouseClicked", "SQL SYNTAXIS", AsociacionRA.class,
                    "SQLException");
            }
        }
        try {
            codRobot = lista.getInt(1);
        } catch (SQLException ex) {
        Sistema.loggear(35, "llenar", "SQL SYNTAXIS", AsociacionRA.class,
                    "SQLException");
        }
}//GEN-LAST:event_robot_tableMouseClicked

    private void acc_tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_acc_tablaMouseClicked
        JTable target = (JTable)evt.getSource();
        int row = target.getSelectedRow();
        int column = target.getSelectedColumn();
        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select a.id \"Codigo\"," 
               + " a.nombre \"Nombre\", a.caracteristicas \"Caracteristicas\" "
               + "from ACCESORIO a");
        } catch (DinamicaDato ex) {
        }
        int i = 0;
        while (i <= row){
            try {
                lista.next();
                i++;
            } catch (SQLException ex) {
        Sistema.loggear(35, "llenar", "SQL SYNTAXIS", AsociacionRA.class,
                    "SQLException");
            }
        }
        try {
            codAccesorio = lista.getInt(1);
        } catch (SQLException ex) {
        Sistema.loggear(35, "llenar", "SQL SYNTAXIS", AsociacionRA.class,
                    "SQLException");
        }
}//GEN-LAST:event_acc_tablaMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Accesorio parte = new Accesorio();
        try {
            parte.asociar(codRobot, codAccesorio);
        } catch (ProcesamientoDato ex) {
        }
        this.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        llenar(robot_table, "select r.id \"Codigo\", r.nombre \"Nombre\"," +
                " r.descripcion \"Descripcion\" from ROBOT r");
      llenar(acc_tabla, "select a.id \"Codigo\", a.nombre \"Nombre\"," +
              " a.caracteristicas \"Caracteristicas\" from ACCESORIO a");
    }//GEN-LAST:event_formWindowOpened

private void jTable1SelectionChanged1(javax.swing.event.ListSelectionEvent evt){


        if (this.robot_table.getRowCount()>0 &&
                !this.robot_table.getSelectionModel().getValueIsAdjusting() &&
                this.robot_table.getSelectedRow()>-1){

            this.fila = robot_table.getSelectedRow();



                String id =robot_table.getValueAt(this.fila, 0).toString();



        }
    }

private void jTable1SelectionChanged2(javax.swing.event.ListSelectionEvent evt){


        if (this.acc_tabla.getRowCount()>0 &&
                !this.acc_tabla.getSelectionModel().getValueIsAdjusting() &&
                this.acc_tabla.getSelectedRow()>-1){

            this.fila = acc_tabla.getSelectedRow();



                String id =acc_tabla.getValueAt(this.fila, 0).toString();



        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AsociacionRA().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable acc_tabla;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable robot_table;
    // End of variables declaration//GEN-END:variables

}
